<template>
  <el-container>
    <el-header>
      <div class="title-block">
        <div class="title-box">
          <span style="font-size: 22px; color: #fff"
            >文件递交系统 <i class="el-icon-s-promotion"></i
          ></span>
        </div>
      </div>
      <ul class="exit-block">
        <li class="exit-item" @click="logout">
          <i class="el-icon-switch-button"></i> 注销
        </li>
      </ul>
    </el-header>
    <el-main>
      <div class="content-header">
        <el-row class="row">
          <el-col :span="24">
            <div class="header-section">
              <h1 class="inline">任务详情</h1>
              <h5 class="inline">
                -
                <a href="" id="task-name" @click.prevent="changeTitle">
                  {{ this.task.title }}</a
                >
              </h5>
              <span id="task-status" class="label">{{ this.task.status }}</span>
              <a
                id="task-period"
                href=""
                class="inline"
                style="margin-left: 20px"
                @click.prevent="changeDate"
                >周期: {{ this.task.stime }}~{{ this.task.etime }}</a
              >
              <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
                <span> {{ this.message }}: </span>
                <el-date-picker
                  type="datetime"
                  v-model="deadline"
                  placeholder="选择一个或多个日期"
                  format="yyyy-MM-dd HH:mm:ss"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  :picker-options="endOptions"
                >
                </el-date-picker>
                <span slot="footer" class="dialog-footer">
                  <el-button @click="dialogVisible = false">取 消</el-button>
                  <el-button type="primary" @click="checkDate()"
                    >确 定</el-button
                  >
                </span>
              </el-dialog>
              <span class="pull-right">
                <span id="btn-outer"
                  ><a
                    class="btn btn-success"
                    style="width: 80px"
                    @click.prevent="startTask"
                    ><i
                      :class="[
                        this.task.status === '运行中'
                          ? 'el-icon-video-pause'
                          : 'el-icon-video-play',
                      ]"
                    ></i>
                    {{ this.task.isStart }}</a
                  ></span
                >
                <a class="btn btn-danger" @click.prevent="deleteTask">删除</a>
              </span>
            </div>
          </el-col>
        </el-row>
      </div>
      <el-row>
        <el-col :span="20" :offset="2">
          <div class="block full">
            <div class="block-title">
              <h2>任务描述</h2>
            </div>
            <div class="block full">
              <el-col :span="24" style="float: none">
                <p class="well">
                  {{ task.description }}
                </p>
              </el-col>
            </div>
          </div>

          <div class="block full">
            <div class="block-title">
              <h2>文档详情</h2>
            </div>
            <div class="block full">
              <el-col :span="24" style="float: none">
                <p class="well">
                  <span
                    v-for="(item, index) in task.word"
                    :key="item"
                    :offset="index"
                  >
                    <a href="" @click.prevent="fileDownload(item.word)">{{
                      item.word
                    }}</a> <br/>
                  </span>
                </p>
              </el-col>
            </div>
          </div>
        </el-col>
      </el-row>
    </el-main>
  </el-container>
</template>
<script>
import {
  getTask,
  changeTaskName,
  changeDate,
  start,
  pause,
} from "@/api/admin.js";
import { formatDate } from "@/utils/dateformat.js";
import { downTaskFile } from "@/api/file.js";
import { countSub, countDSub} from "@/api/brand.js";
export default {
  data() {
    return {
      tid: null,
      task: null,
      dialogVisible: false,
      deadline: "",
      message: "",
      warn: "",
      endOptions: {
        disabledDate: (time) => {
          return time.getTime() <= new Date(this.task.stime).getTime();
        },
      },
    };
  },

  created() {
    this.tid = this.$route.query.tid;
    this.getTaskInfo();
  },

  methods: {
    
    logout() {
      this.$store.dispatch("LogOut").then((response) => {});
      this.$router.push({
        path: "/login",
      });
    },
    checkDate() {
      changeDate(this.tid, this.deadline).then((response) => {
        this.getTaskInfo();
      });
          
    },
    changeTitle() {
      this.$prompt("请输入新的任务标题", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          if(!value){
            this.$message({
              type: "warn",
              message: "任务名称不能为空",
            });
          }else if(value.length > 10){
            this.$message({
              type: "warn",
              message: "任务名称不能超过10个字符",
            });
          }else{
            changeTaskName(this.tid, value).then((response) => {
            this.$message({
              type: "success",
              message: "已将任务标题修改为: " + value,
            });
            this.getTaskInfo();
            });
          }         
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    changeDate() {
      this.message = "请选择新的截至日期";
      this.dialogVisible = true;
    },
    startTask() {
      if (this.task.status === "已结束") {
        this.message = "该任务已结束，选择新的截至日期以开始任务   ";
        this.dialogVisible = true;
      } else if (this.task.status === "运行中") {
        pause(this.tid).then((response) => {
          this.task.status = "暂停中";
          this.task.isStart = "运行";
          this.getTaskInfo();
        });
      } else {
        start(this.tid, "").then((response) => {
          this.task.status = "运行中";
          this.task.isStart = "暂停";
          this.getTaskInfo();
        });
      }
    },
    deleteTask() {
      this.$confirm("是否永久删除任务: " + this.task.title + "?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delTask(this.task.id).then((response) => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      });
    },
    fileDownload(filename) {
      let params = new URLSearchParams();
      params.append("filename", filename);
      downTaskFile(params).then((response) => {
        const blob = new Blob([response], { type: "text/pain;charset=utf-8" });
        const url = window.URL.createObjectURL(blob);
        const a = document.createElement("a");
        a.download = filename + ".docx";
        a.href = url;
        a.click();
        window.URL.revokeObjectURL(url);
      });
    },
    getTaskInfo() {
      getTask(this.tid).then((response) => {
        let data = response.data;
        this.task = {};
        this.task.id = data.id;
        this.task.description = data.description;
        this.task.word = data.words;
        this.task.title = data.title;
        this.task.status = this.getTaskStatus(data.status);
        //console.log(this.task.status);
        this.task.stime = data.createTime;
        this.task.etime = data.deadline;
        this.task.isStart = this.task.status === "运行中" ? "暂停" : "运行";
        
      });
    },
    getTaskStatus(status) {
      var name = "";
      switch (status) {
        case 0:
          name = "未开始";
          break;
        case 1:
          name = "运行中";
          break;
        case 2:
          name = "已结束";
          break;
        case 3:
          name = "暂停中";
          break;
      }
      return name;
    },
  },
};
</script>
<style scoped>
.el-header {
  width: 100%;
  background: #009688;
  position: fixed;
  z-index: 1030;
}
.exit-block {
  position: absolute;
  right: 10px;
  top: 0;
}

.exit-item {
  list-style: none;
  color: white;
  margin: 0 15px;
  line-height: 30px;
}

.title-box {
  left: 0;
  width: 300px;
  line-height: 60px;
}

.title-block {
  align-items: center;
  width: 185px;
  height: 70px;
}

.el-main {
  min-height: 472px;
  padding: 20px 20px 1px;
  padding-top: 80px;
  background-color: #eeebf2;
}

.content-header {
  background-color: #ffffff;
  border-bottom: 2px solid #dae0e8;
  border-bottom-color: rgb(218, 224, 232);
  margin: -10px -5px 10px;
  padding: 0 15px;
  margin: -20px -20px 20px;
  border-bottom-color: #dfdae8;
  padding-left: 20px;
  padding-right: 20px;
}

.row {
  margin-left: -15px;
  margin-right: -15px;
}

.header-section {
  padding: 15px 0px;
}

.content-header h1,
h2 {
  margin: 5px 0;
  line-height: 28px;
}

.content-header h1 {
  font-weight: 500;
  font-size: 24px;
}

.label {
  display: inline;
  padding: 0.2em 0.6em 0.3em;
  font-size: 75%;
  font-weight: bold;
  line-height: 1;
  color: #fff;
  text-align: center;
  white-space: nowrap;
  vertical-align: baseline;
  border-radius: 0.25em;
  font-weight: normal;
  font-size: 90%;
  padding: 1px 4px;
  margin-left: 40px;
  color: black;
}

.well {
  min-height: 20px;
  padding: 19px;
  margin-bottom: 20px;
  background-color: #f5f5f5;
  border: 1px solid #e3e3e3;
  border-radius: 4px;
  -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05);
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05);
}

.well {
  background-color: #f9f9f9;
  border: 1px solid #eeeeee;
}
p {
  line-height: 1.7;
}

a {
  text-decoration: none;
  color: #7c62ad;
}
</style>